package com.egg.service.impl;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.egg.dao.BaseDao;
import com.egg.service.LoginService;
/**
 * @项目名称：egg   
 * @类名称：LoginServiceImpl   
 * @类描述：登录实现   
 * @创建人：wangchenyang
 * @创建时间：2012-9-3 下午8:08:59   
 * @修改人：
 * @修改时间：   
 * @修改备注：   
 * @version
 */
@Service
public class LoginServiceImpl implements LoginService {

	@Autowired
	BaseDao baseDao;
	
	@Override
	public List<Map<String, Object>> loginEgg(HttpSession session,String userAccount,String password) {
		List<Map<String,Object>> resultList=null;
		Object[] obj={userAccount,password};
		String sql1="SELECT * FROM EGG_USER WHERE USER_ACCOUNT=? AND USER_PASSWORD=?";
		List<Map<String,Object>> list=baseDao.queryForList(sql1,obj);
		//判断用户是否存在
		if(list!=null&&list.size()!=0){
			String sql2="SELECT EM.MENU_NAME,EM.MENU_URL,EM.PID,EM.DEPT_ID FROM EGG_USER_ROLE EUR,EGG_MENU_ROLE EMR,EGG_MENU EM WHERE EUR.ROLE_ID=EMR.ROLE_ID AND EUR.USER_ID=? AND EMR.MENU_ID=EM.ID";
			String deptId=String.valueOf(list.get(0).get("DEPT_ID"));
			String userId=String.valueOf(list.get(0).get("ID"));
			String userName=String.valueOf(list.get(0).get("USER_NAME").toString());
			String userType=String.valueOf(list.get(0).get("USER_TYPE").toString());
			//获得用户能够访问的菜单
			resultList=baseDao.queryForList(sql2,userId);
			//将用户信息放入session
			session.setAttribute("userId", userId);
			session.setAttribute("deptId", deptId);
			session.setAttribute("menu", resultList);
			session.setAttribute("userName", userName);
			session.setAttribute("userType", userType);
		}
		return resultList;
	}

	@Override
	public void logoutEgg(HttpSession session) {
		session.setAttribute("userId", null);
		session.setAttribute("deptId", null);
		session.setAttribute("menu", null);
		session.setAttribute("userName", null);
	}

}
